package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotApplyMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotApply;
import io.xxx.xbutler.domain.task.ReceiverType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 私聊消息发送结果回调接口（兼容PC）
 *
 * <pre>
 * {
 *     "Data": {
 *         "MsgData": [
 *             {
 *                 "vcDesc": null,
 *                 "vcHref": null,
 *                 "nMsgNum": 1,
 *                 "vcTitle": null,
 *                 "nMsgType": 2001,
 *                 "dtMsgTime": "2025-02-05 09:58:17.000",
 *                 "nEmojiLen": 0,
 *                 "msgContent": "8J+Ogee0oOadkOS4iuS8oOivtOaYju+8mgox77iP4oOj57uZ5oiR5Y+R6YCB44CQ5byA5aeL44CR5oyH5Luk77yM5pS25Yiw4oCc5byA5aeL57Sg5p2Q5o6l5pS24oCd5Zue5aSN5ZCO77yM5L2g5bCx5Y+v5Lul5LiK5Lyg57Sg5p2Q5LqGCjLig6PvuI/lj5HpgIHllYblk4HnmoTmlofmoYjjgIHlm77niYfjgIHllYblk4HljaHniYfnu5nmiJHvvIzljZXmrKHku4XmlK/mjIHkuIDkuKrllYblk4HntKDmnZDkuIrkvKAKM+KDo++4j+WPkemAgeOAkOe7k+adn+OAkeaMh+S7pO+8jOaUtuWIsOKAnOe0oOadkOS4iuS8oOaIkOWKn+KAneWbnuWkjeWQjue7k+adn+acrOasoee0oOadkOS4iuS8oOOAgg==",
 *                 "nVoiceTime": 0,
 *                 "vcEmojiMd5": null
 *             }
 *         ],
 *         "nMsgNum": 1,
 *         "vcMsgId": "1524935400890203039",
 *         "vcErrorMsgData": null,
 *         "vcRelaSerialNo": null,
 *         "vcToWxUserWxId": "wxid_bysnjh4y1f1922",
 *         "vcToWxUserSerialNo": "DA1B048A0F72A1142FE1F29C982E3B79"
 *     },
 *     "nType": 5004,
 *     "nResult": 1,
 *     "vcResult": "消息发送成功",
 *     "nTimeStamp": 1738720697172,
 *     "vcSerialNo": "20250205095816181024511171361",
 *     "vcRobotWxId": "wxid_mi88feubut7822",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "B9242BBFE29699C36B54435F5F407F2F"
 * }
 * </pre>
 */
@Slf4j
@Component
public class _5004Handler extends SendChatMessagesHandler {

    @Resource
    private PushResultHandler pushResultHandler;

    @Resource
    private RobotApplyMapper robotApplyMapper;

    @Override
    public String name() {
        return "私聊消息发送结果回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 5004;
    }

    @Override
    protected void handle0(String actionType, String actionId, Callback callback, JSONObject data) {
        long id = Long.parseLong(actionId);
        switch (actionType) {
            case "bindStore":
                robotApplyHandle(id);
                break;
            case "push":
                pushResultHandler.handle(callback, id, ReceiverType.CONTACT, data.getString("vcToWxUserWxId"));
                break;
            default:
                log.error("未知的消息发送类型[id:{}, serialNo:{}, action:{}]", callback.getId(), callback.getSerialNo(), actionType);
        }
    }

    private void robotApplyHandle(Long id) {
        LambdaUpdateWrapper<RobotApply> wrapper = new LambdaUpdateWrapper<RobotApply>()
                .set(RobotApply::getSendBind, true)
                .set(RobotApply::getUpdatedTime, LocalDateTime.now())
                .eq(RobotApply::getId, id);
        robotApplyMapper.update(wrapper);
    }
}
